package com.src.xyzk_personal.config;

import com.src.xyzk.bluetooth.service.BluetoothAdapterService;

public class Communication {
	private static final String TAG = "Communication";
	static int m_mode = Common.Communication;
	static boolean exit = false;
	private static  Communication m_com;
	BluetoothAdapterService bcom = null;
	JCommunication Jcom = null;
	public LogFile m_log = null;

	private Communication()
	{
		m_log = LogFile.getInstance();
		if(m_mode == 0) //blue
			bcom = BluetoothAdapterService.getInstance();
		else if(m_mode == 1) //cable
			Jcom = JCommunication.getInstance();
		if(m_log != null)
			m_log.PrintLog(TAG + " mode:" + m_mode);
	}
	public synchronized static Communication getInstance(){
		return m_com == null ? m_com = new Communication() : m_com;
	}
	//can
	public int CanOne2OneUDS(int Preq,int Pres,byte [] Send,int Plen,byte [] Recv,int Perror,int Pmaxtime,int Pfreq)
	{
		if(m_mode == 0)
			return bcom.CanOne2OneUDS(Preq, Pres, Send, Plen, Recv, Perror, Pmaxtime, Pfreq);
		else
			return Jcom.CanOne2OneUDS(Preq, Pres, Send, Plen, Recv, Perror, Pmaxtime, Pfreq);
	}
	public int CanSendOneOnly(int SendId,byte[] send,int Plen)
	{
		if(m_mode == 0)
			return bcom.CanSendOneOnly(SendId, send, Plen);
		else
			return Jcom.CanSendOneOnly(SendId, send, Plen);
	}
	public int CanSendAndRecvO2O_Spec(int Psid,int Prid,byte [] Send,int Plen,byte [] Recv,int Psize,int errorcode,int maxtime)
	{
		if(m_mode == 0)
			return bcom.CanSendAndRecvO2O_Spec(Psid,Prid,Send,Plen,Recv,Psize,errorcode,maxtime);
		else
			return Jcom.CanSendAndRecvO2O_Spec(Psid,Prid,Send,Plen,Recv,Psize,errorcode,maxtime);
	}
	public String ReadBoxVersion()
	{
		byte [] v_buf = new byte[32];
		byte [] v_soft = new byte[32];
		for(int i = 0; i < 32; i ++)
		{
			v_buf[i] = 0;
			v_soft[i] = 0;
		}

		if(m_mode == 0)
		{
			if(bcom.GetSerialNum(	v_buf, 32, v_soft) == true)
				return new String(v_buf) + "_" + new String(v_soft);
			else
				return "";
		}
		else
			return Jcom.ReadVersion();
	}
	public boolean SetCommunTimer(int P1,int P2,int P3,int P4)
	{
		if(m_mode == 0)
			return bcom.SetCommunTimer(P1, P2, P3, P4);
		else
			return Jcom.SetCommunTimer(P1, P2, P3, P4);
	}
	public boolean SetCanBaud(long baud)
	{
		if(m_mode == 0)
			return bcom.SetCanBaud(baud);
		else
			return Jcom.SetCanBaud(baud);
	}
	public boolean SetCanMode(int Pmode)
	{
		if(m_mode == 0)
			return bcom.SetCanMode(Pmode);
		else
		{
			if(Pmode == 2)
				Pmode = 1;
			else
				Pmode = 2;
			return Jcom.SetCanMode(Pmode);
		}
	}
	public boolean SetCanFilter(byte [] Pbuf,int num)
	{
		if(m_mode == 0)
			return bcom.SetCanFilter(Pbuf,num);
		else
			return Jcom.SetCanFilter(Pbuf,num);
	}
	public boolean SetCanAutoFrame(byte [] Pbuf,int Plen)
	{
		if(m_mode == 0)
			return bcom.SetCanAutoFrame(Pbuf,Plen);
		else
			return Jcom.SetCanAutoFrame(Pbuf,Plen);
	}
	//设置CAN自动回复
	public boolean SetStandCanAutoFrame(int Pid,byte [] Pbuf,int Plen)
	{
		if(m_mode == 0)
			return bcom.SetStandCanAutoFrame(Pid,Pbuf,Plen);
		else
			return Jcom.SetStandCanAutoFrame(Pid,Pbuf,Plen);
	}
	public String GetLogfileName()
	{
		if(m_mode == 0)
			return bcom.GetLogfileName();
		else
			return Jcom.GetLogfileName();
	}
	public String getFilePaths()
	{
		if(m_mode == 0)
			return bcom.getFilePaths();
		else
			return Jcom.getFilePaths();
	}
}
